using System;
using System.Collections.Generic;

namespace Developex.DatabaseWrapper.CSVImportExport
{

	///<summary>
	///</summary>
	public class EIField
	{
		/// <summary>
		/// 
		/// </summary>
		public Dictionary<string, int> ImportHeadersByName = null;

		/// <summary>
		/// 
		/// </summary>
		public string[] ImportValuesString = null;

		/// <summary>
		/// 
		/// </summary>
		public string Name;

		private bool valueSet = false;

		/// <summary>
		/// 
		/// </summary>
		protected object value = null;

		///<summary>
		///</summary>
		public virtual object Value
		{
			get
			{
				if (!valueSet && ImportHeadersByName != null && ImportValuesString != null)
					return ImportValuesString[ImportHeadersByName[DisplayName]];
				return value;
			}
			set
			{
				valueSet = true;
				this.value = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		protected string displayName = "";
		/// <summary>
		/// 
		/// </summary>
		protected int? position = null;

		///<summary>
		///</summary>
		public int? Position
		{
			get { return position; }
			set { position = value; }
		}

		///<summary>
		///</summary>
		///<param name="name"></param>
		public EIField(string name)
		{
			this.Name = name;
			this.DisplayName = name;
		}

		///<summary>
		///</summary>
		///<param name="name"></param>
		///<param name="value"></param>
		public EIField(string name, object value)
			: this(name, value, name)
		{
		}

		///<summary>
		///</summary>
		///<param name="name"></param>
		///<param name="value"></param>
		///<param name="displayName"></param>
		public EIField(string name, object value, string displayName)
			: this(name, value, displayName, null)
		{
		}

		///<summary>
		///</summary>
		///<param name="name"></param>
		///<param name="value"></param>
		///<param name="displayName"></param>
		///<param name="position"></param>
		public EIField(string name, object value, string displayName, int? position)
		{
			this.Name = name;
			this.value = value;
			valueSet = true;
			this.DisplayName = displayName;
			this.Position = position;
		}

		///<summary>
		///</summary>
		public string DisplayName
		{
			get
			{
				if (!String.IsNullOrEmpty(displayName))
					return this.displayName;

				if (this.Name.IndexOf('.') == -1)
				{
					return Name;
				}

				return Name.Substring(Name.LastIndexOf('.'), Name.Length - Name.LastIndexOf('.'));
			}
			set
			{
				this.displayName = value;
			}
		}
	}
}